<?php

/**
 * This is the model class for table "tbl_dropdowns".
 *
 * The followings are the available columns in table 'tbl_dropdowns':
 * @property integer $id
 * @property string $type
 * @property string $name
 * @property integer $option
 * @property integer $inactive
 * @property integer $sort
 */
class Dropdowns extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Dropdowns the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_dropdowns';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id, type, name, option, active, sort', 'required'),
			array('id, option, inactive, sort', 'numerical', 'integerOnly'=>true),
			array('type', 'length', 'max'=>25),
			array('name', 'length', 'max'=>50),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, type, name, option, inactive, sort', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'type' => 'Type',
			'name' => 'Name',
			'option' => 'Option',
			'inactive' => 'Inactive',
			'sort' => 'Sort',
		);
	}

    
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('option',$this->option);
		$criteria->compare('inactive',$this->active);
		$criteria->compare('sort',$this->sort);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
    public static function getText($type,$option){
        $text= Yii::app()->db->createCommand('select name from tbl_dropdowns where type=:type and `option`=:option')->queryScalar(array(
            'type'=>$type,
            'option'=>$option
        ));
        if($text !==false){
        	return $text;
        }
        return '';
    }

    public static  function getList($type){
        $criteria = new CDbCriteria();        
        $criteria->addColumnCondition(array('inactive'=>'0'));
        $criteria->compare('type',$type);
        $criteria->order = "sort";
        $list = self::model()->findAll($criteria);
        $rs = array();
        foreach($list as $item){
            $rs[$item->option] = $item->name;
        }
        return $rs;

    }
    public static function getDispatchBillTypeList(){
    	$rs = array(null=>'All');
    	$rs += self::getList('DispatchBill');
    	return $rs;
    }
}